from django.db import models

# Create your models here.


from base.models import BaseModel


# 矿物种类
class MineralType(BaseModel):
    name = models.CharField(max_length=128, verbose_name='种类')

    # 大类库存  同类型货品相加 便于统计数据
    quantity = models.DecimalField(verbose_name='总数量', max_digits=15, decimal_places=2, blank=True, null=True)

    # 安全天数  按照大类计算
    safe_day = models.DecimalField(verbose_name='安全天数', max_digits=15, decimal_places=2, blank=True, null=True,
                                   default=20)

    class Meta:
        db_table = 'mineral_type'
        verbose_name = '矿物种类'
        verbose_name_plural = verbose_name


# 货品统计分组
class ProductGroup(BaseModel):
    # 种类名称 用于区分 所属哪个种类
    type_name = models.CharField(max_length=128, verbose_name='种类名称')

    # 统计分组的每日消耗量
    daily_consumption = models.DecimalField(verbose_name='每日消耗量', max_digits=15, decimal_places=2, blank=True,
                                            null=True, default=0)

    # 统计分组厂内库存总数 用于生成统计报表
    plant_quantity = models.DecimalField(verbose_name='库存总数量', max_digits=15, decimal_places=2, blank=True, null=True,
                                         default=0)

    # 厂内库存数量 同类型货品 厂内库存 + 到港库存 用于生成统计报表
    in_plant_quantity = models.DecimalField(verbose_name='厂内库存总数量', max_digits=15, decimal_places=2, blank=True,
                                            null=True, default=0)

    #  计划生成用量 用于生成统计报表
    requirement_planning = models.DecimalField(verbose_name='计划生产用量/月', max_digits=15, decimal_places=2, blank=True,
                                               null=True, default=0)

    class Meta:
        db_table = 'product_group'
        verbose_name = '货品统计分组'
        verbose_name_plural = verbose_name


# 货品名
class Product(BaseModel):
    name = models.CharField(max_length=128, verbose_name='货品名称', blank=True, null=True)
    mineral_type = models.ForeignKey(MineralType, on_delete=models.PROTECT, verbose_name='种类', blank=True, null=True)
    product_group = models.ForeignKey(ProductGroup, on_delete=models.PROTECT, verbose_name='货品统计分组', blank=True,
                                      null=True)

    # 不属于统计分组时 使用这个作为 每日消耗量
    daily_consumption = models.DecimalField(verbose_name='每日消耗量', max_digits=15, decimal_places=2, blank=True,
                                            null=True, default=0)

    class Meta:
        db_table = 'product'
        verbose_name = '货品名'
        verbose_name_plural = verbose_name


# 厂内库存执行表   记录库存增减
class InventoryPerform(BaseModel):
    product = models.ForeignKey(Product, on_delete=models.PROTECT, verbose_name='货品', )  # blank=True, null=True
    operation_type = models.CharField(max_length=128, verbose_name='操作类型', blank=True, null=True)
    # 增减数量
    quantity = models.DecimalField(verbose_name='数量/吨', max_digits=15, decimal_places=2, blank=True, null=True)

    # 领用后库存  记录库存变化
    inventory_after_requisition = models.DecimalField(verbose_name='领用后库存', max_digits=15, decimal_places=2, blank=True,
                                                      null=True)

    # 出入库时间
    storage_time = models.DateField(verbose_name='出入库时间', blank=True, null=True)
    note = models.CharField(blank=True, null=True, max_length=128, verbose_name='备注')

    class Meta:
        db_table = 'inventory_perform'
        verbose_name = '厂内库存执行表'
        verbose_name_plural = verbose_name


# 库存表 到每个货品
class Inventory(BaseModel):
    product = models.ForeignKey(Product, on_delete=models.PROTECT, verbose_name='货品', blank=True, null=True)
    plant_quantity = models.DecimalField(verbose_name='厂内库存数量', max_digits=15, decimal_places=2, blank=True, null=True,
                                         default=0)
    port_quantity = models.DecimalField(verbose_name='到港未到厂数量', max_digits=15, decimal_places=2, blank=True, null=True,
                                        default=0)
    not_arrival_port_quantity = models.DecimalField(verbose_name='未到港数量', max_digits=15, decimal_places=2, blank=True,
                                                    null=True, default=0)
    # 用于说明到港大概时间
    not_arrival_port_note = models.CharField(blank=True, null=True, max_length=128, verbose_name='未到岗备注')

    # 安全天数 冗余储存 同  MineralType.safe_day
    safe_day = models.DecimalField(verbose_name='安全天数', max_digits=15, decimal_places=2, blank=True, null=True,
                                   default=20)
    # 冗余储存 同 ProductionPlan.daily_consumption
    daily_consumption = models.DecimalField(verbose_name='每日消耗量', max_digits=15, decimal_places=2, blank=True,
                                            null=True)

    @property
    def total_quantity(self):
        '''合计数量'''
        # 合计数量 返回 厂内库存数量 + 到港未到厂 + 未到港
        return self.plant_quantity + self.port_quantity + self.not_arrival_port_quantity

    @property
    def safe_quantity(self):
        # 安全数量 返回 安全天数 * 每日消耗量
        return self.safe_day * self.daily_consumption

    class Meta:
        db_table = 'inventory'
        verbose_name = '库存表'
        verbose_name_plural = verbose_name


# 生产计划表
class ProductionPlan(BaseModel):
    product = models.ForeignKey(Product, on_delete=models.PROTECT, verbose_name='货品', blank=True, null=True)
    daily_consumption = models.DecimalField(verbose_name='每日消耗量', max_digits=15, decimal_places=2, default=0)
    days_available = models.DecimalField(verbose_name='可用天数', max_digits=15, decimal_places=2, blank=True, null=True)
    mineral_type = models.ForeignKey(MineralType, on_delete=models.PROTECT, verbose_name='种类', blank=True, null=True)

    class Meta:
        db_table = 'production_plan'
        verbose_name = '生产计划表'
        verbose_name_plural = verbose_name


# 采购计划表
class PurchasePlan(BaseModel):
    product = models.ForeignKey(Product, on_delete=models.PROTECT, verbose_name='货品', blank=True, null=True)
    last_parchase_date = models.DateField(verbose_name='最晚进料日期', blank=True, null=True)
    purchase_quantity = models.DecimalField(verbose_name='计划生产用量/月', max_digits=15, decimal_places=2, blank=True,
                                            null=True, default=0)

    # 实际执行的采购计划
    actual_purchase_quantity = models.DecimalField(verbose_name='实际计划采购', max_digits=15, decimal_places=2, blank=True,
                                                   null=True, default=0)

    class Meta:
        db_table = 'purchase_plan'
        verbose_name = '采购计划表'
        verbose_name_plural = verbose_name
